/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package wh.server;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import wh.*;

/**
 *
 * @author Dave
 */
public class Functions {
    public static void login(Player p) {
				System.out.println("Login called");
        //Will probably need to access database at some point
        DataInput stream = p.getReader().getDataInput();
				System.out.println("Got input stream");
        DataOutput outStream = p.getWriter().getStream();
				System.out.println("Got output stream");
        String name = "";
        try {
            //short numBytes = stream.readByte();
            //name = stream.readUTF();
						System.out.println("Name: " + name);
            //String pass = stream.readUTF();
            int i = stream.readInt();
						System.out.println("Int: " + i);
            short l = stream.readShort();
						System.out.println("Short1: "  + l);
            short k = stream.readShort();
						System.out.println("Short2: "  + k);
						System.out.println("All fields gathered");
        } catch (IOException ex) {
            Logger.getLogger(Functions.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            //Write the output to client -> convert to function
            outStream.writeInt(1); //User id
            outStream.writeShort(100); //Credits
            outStream.writeByte(1); //Subscription level
            outStream.writeUTF(name); //User name
            p.getWriter().sendPacket();
						System.out.println("Packet sent");
        } catch (IOException ex) {
            Logger.getLogger(Functions.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void joinTable(Player p) {
        try {
            DataInput stream = p.getReader().getDataInput();
            //short numBytes = stream.readShort();
            short tableID = stream.readShort();
            String username = stream.readUTF();
        } catch (IOException ex) {
            Logger.getLogger(Functions.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void whisper(Player p) {
        try {
            DataInput stream = p.getReader().getDataInput();
            //short numBytes = stream.readShort();
            String userTo = stream.readUTF();
            String msg = stream.readUTF();
        } catch (IOException ex) {
            Logger.getLogger(Functions.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void createTable(Player p) {
        DataInput stream = p.getReader().getDataInput();
        //short numBytes = stream.readShort();

    }

    public static void tableSay(Player p){
        DataInput stream = p.getReader().getDataInput();
        try {
            String message = stream.readUTF();
        } catch (IOException ex) {
            Logger.getLogger(Functions.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void changeTeams(Player p) {
        DataInput stream = p.getReader().getDataInput();
        try {
            short i = stream.readShort();
        } catch (IOException ex) {
            Logger.getLogger(Functions.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void listUserNames(Player p) {
        DataInput stream = p.getReader().getDataInput();
        try {
            //Should == -2
            short i = stream.readShort();
        } catch (IOException ex) {
            Logger.getLogger(Functions.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void listTables(Player p) {
        DataInput stream = p.getReader().getDataInput();
        try {
            //Should == -2
            short i = stream.readShort();
        } catch (IOException ex) {
            Logger.getLogger(Functions.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void leaveTable(Player p) {
        DataInput stream = p.getReader().getDataInput();
        try {
            //Should == -2
            short i = stream.readShort();
        } catch (IOException ex) {
            Logger.getLogger(Functions.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void startGame(Player p) {
        DataInput stream = p.getReader().getDataInput();
        try {
            short i = stream.readShort();
        } catch (IOException ex) {
            Logger.getLogger(Functions.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static void special(Player p) {
        DataInput stream = p.getReader().getDataInput();
        short secondOp = 0;
        try {
            secondOp = stream.readShort();
        } catch (IOException ex) {
            Logger.getLogger(Functions.class.getName()).log(Level.SEVERE, null, ex);
        }
        switch(secondOp) {
            case 2: //Noop
                break;
            case 27: //Submit credits
                break;
            case 5: //Say
                break;
            default:
            //case gameid: 106
            //handle updateState
            //case gameid: 110
            //handle gameOver
            //case gameid: 109
            //handle updateEvent
            //case gameid: 107
            //handle usePowerup

        }
    }

    private static void updateState(Player p) {
        DataInput stream = p.getReader().getDataInput();
        try {
            short i = stream.readShort(); //Gameid?
            //writeState
        } catch (IOException ex) {
            Logger.getLogger(Functions.class.getName()).log(Level.SEVERE, null, ex);
        }
        //writeState
    }

    private static void writeState(Player p) {
        try {
            DataInput stream = p.getReader().getDataInput();
            short healthPercent = stream.readShort();
            byte numPowerups = stream.readByte();
            byte[] powerup = new byte[numPowerups];
            for (numPowerups--; numPowerups >= 0; numPowerups--) {
                powerup[numPowerups] = stream.readByte();
            }
            byte fighterType = stream.readByte();
            //If next byte is zero, end
            if (stream.readByte() == 0) {
                return;
            } else {
                byte damagingPlayer = stream.readByte();
                byte damagingPowerup = stream.readByte();
                byte damagedHealth = stream.readByte();
            }
        } catch (IOException ex) {
            Logger.getLogger(Functions.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static void gameOver(Player p) {
        try {
            DataInput stream = p.getReader().getDataInput();
            short i = stream.readShort();
            byte b = stream.readByte();
        } catch (IOException ex) {
            Logger.getLogger(Functions.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static void updateEvent(Player p) {
        try {
            DataInput stream = p.getReader().getDataInput();
            short i = stream.readShort();
            String utf = stream.readUTF();
        } catch (IOException ex) {
            Logger.getLogger(Functions.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static void usePowerup(Player p) {
        try {
            DataInput stream = p.getReader().getDataInput();
            short i = stream.readShort();
            byte b = stream.readByte();
            byte c = stream.readByte();
            byte d = stream.readByte();
        } catch (IOException ex) {
            Logger.getLogger(Functions.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
